// pages/liveDayComment/liveDayComment.js
const app = getApp();
var utils = require("../../utils/utils.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    qualityList: [{}, {}],
    texts: "至少10个字",
    min: 10,
    dictDataValue: "",
    showModalStatus: false,
    comment: ""
  },
  powerDrawer: function(e) {
    var showModalStatus = this.data.showModalStatus ? false : true;
    this.setData({ showModalStatus: showModalStatus});
    this.util(e.currentTarget.dataset.status)
  },
  util: function(currentStatu) {
    /* 动画部分 */
    // 第1步：创建动画实例 
    var animation = wx.createAnimation({
      duration: 200, //动画时长 
      timingFunction: "linear", //线性 
      delay: 0 //0则不延迟 
    });

    // 第2步：这个动画实例赋给当前的动画实例 
    this.animation = animation;

    // 第3步：执行第一组动画 
    animation.opacity(0).rotateX(-100).step();

    // 第4步：导出动画对象赋给数据对象储存 
    this.setData({
      animationData: animation.export()
    })
    // 第5步：设置定时器到指定时候后，执行第二组动画 
    setTimeout(function() {
      // 执行第二组动画 
      animation.opacity(1).rotateX(0).step();
      // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象 
      this.setData({
        animationData: animation
      })

      //关闭 
      if (currentStatu == "close") {
        this.setData({
          showModalStatus: false
        });
      }
    }.bind(this), 200)
    // 显示 
    if (currentStatu == "open") {
      this.setData({
        showModalStatus: true
      });
    }
  },
  addList: function() {
    var qualityList = this.data.qualityList;
    // 面向
    var newData = {
      dictDataValue: this.data.dictDataValue,
      isCheck: true
    };
    qualityList.push(newData); //实质是添加lists数组内容，使for循环多一次
    this.setData({
      qualityList: qualityList,
      showModalStatus: false
    })
  },
  label: function(e) {
    this.setData({
      dictDataValue: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.queryKeywords();
    this.queryChildActInfo(options.activityId, options.childId);
  },
  queryChildActInfo: function(activityId, childId) {
    var that = this;
    // 查询小孩参与的活动信息
    wx.request({
      url: app.httpUrl + '/activity/tutor/queryChildActInfo',
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      method: "POST",
      data: {
        activityId: activityId,
        childId: childId
      },
      success: (res) => {
        if (res.data.code == '000') {
          var data = res.data.data;
          that.setData({
            childActInfo: data
          });
          var currTime = new Date().getTime();
          var currDate, totalDay = 0,
            dateIndex = 0;
          if (currTime > data.endDate) {
            currDate = utils.formatDate(data.endDate, 'YMD');
            totalDay = parseInt(Math.ceil((data.endDate - data.startDate) / 1000 / 60 / 60 / 24))
          } else {
            currDate = utils.formatDate(currTime, 'YMD');
            totalDay = parseInt(Math.ceil((currTime - data.startDate) / 1000 / 60 / 60 / 24))
          }
          var dateList = [];
          for (var i = 0; i < totalDay; i++) {
            var activityDate = data.startDate + ((totalDay - i - 1) * 24 * 60 * 60 * 1000);
            dateList[i] = {
              value: utils.formatDate(activityDate, 'YMD'),
              name: utils.formatDate(activityDate, 'Y年M月D日')
            }
            if (currDate == dateList[i].dateValue) {
              dateIndex = i;
            }
          }
          that.setData({
            dateList: dateList,
            dateIndex: dateIndex
          })
          this.queryDayComment(currDate, data.activityChildId);
        }
      }
    })
  },
  queryDayComment: function(commentDate, activityChildId) {
    var that = this;
    // 查询小孩参与的活动信息
    wx.request({
      url: app.httpUrl + '/activity/tutor/queryChildCommentByDate',
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      method: "POST",
      data: {
        commentDate: commentDate,
        activityChildId: activityChildId
      },
      success: (res) => {
        if (res.data.code == '000') {
          var data = res.data.data;
          var qualityList = that.data.qualityList;
          for (var i in qualityList) {
            qualityList[i].isCheck = false;
          }
          if (data != null) {
            var list = data.keywords.split(",");
            for (var i in list) {
              var isHas = false;
              for (var j in qualityList) {
                if (qualityList[j].dictDataValue == list[i]) {
                  isHas = true;
                  qualityList[j].isCheck = true;
                  break;
                }
              }
              if(!isHas) {
                qualityList.push({dictDataValue: list[i], isCheck: true})
              }
            }
          } else {
            data = [];
          }
          data.commentDate = commentDate;
          that.setData({
            dayComment: data,
            comment: data.comment ? data.comment : "",
            qualityList: qualityList
          });
        }
      }
    })
  },
  queryKeywords: function() {
    let that = this
    // 首页头部地区接口
    wx.request({
      url: app.httpUrl + '/activity/tutor/queryChildQuality',
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      method: "GET",
      success: res => {
        that.setData({
          qualityList: res.data
        })
      }
    })
  },
  submitTap: function() {
    var that = this;
    var comment = that.data.comment;
    if (comment.length < 10) {
      wx.showToast({
        title: '评论内容不少于10个字',
        icon: 'none',
        duration: 3000
      });
      return;
    }
    var qualityList = this.data.qualityList,
      keywords = [];
    for (var i in qualityList) {
      if (qualityList[i].isCheck) {
        keywords.push(qualityList[i].dictDataValue);
      }
    }
    if (keywords.length == 0) {
      wx.showToast({
        title: '请为同学选择几个让你印象深刻的品质',
        icon: 'none',
        duration: 3000
      });
      return;
    }

    wx.request({
      url: app.httpUrl + '/activity/tutor/childCommentSubmit',
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      method: "POST",
      data: {
        id: this.data.dayComment.id,
        activityId: that.data.childActInfo.activityId,
        activityChildId: that.data.childActInfo.activityChildId,
        comment: comment,
        commentDate: this.data.dayComment.commentDate,
        keywords: keywords.toString()
      },
      success: res => {
        if (res.data.code == '000') {
          that.setData({
            dayComment: that.data.data
          })
          wx.showToast({
            title: '提交成功！',
            icon: 'success',
            duration: 3000
          });
          return;
        }
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 3000
        });
      }
    })
  },
  keyworkTap: function(event) {
    var qualityList = this.data.qualityList;
    var idx = event.currentTarget.dataset.idx;
    if (qualityList[idx].isCheck) {
      qualityList[idx].isCheck = false;
    } else {
      qualityList[idx].isCheck = true;
    }
    this.setData({
      qualityList: qualityList
    })
  },
  bindPickerChangeDate: function(e) {
    var idx = e.detail.value;
    this.setData({
      dateIndex: idx
    })
    this.queryDayComment(this.data.dateList[idx].value, this.data.childActInfo.activityChildId);
  },
  formReset: function() {
    this.setData({
      dictDataValue: '',
      showModalStatus: false //让模态框显示
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  //字数限制  
  //字数限制  
  inputs: function (e) {
    // 获取输入框的内容
    var value = e.detail.value;
    // 获取输入框内容的长度
    var len = parseInt(value.length);

    //最少字数限制
    if (len <= this.data.min) {
      this.setData({
        texts: "至少10个字",
        comment: value
      })
    } else if (len > this.data.min) {
      this.setData({
        texts: "",
        comment: value
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})